package com.chamberlain.web.controller.equipment;

import com.chamberlain.common.core.domain.entity.Scene;
import com.chamberlain.common.core.domain.entity.SysUser;
import com.chamberlain.common.core.domain.model.LoginUser;
import com.chamberlain.equipment.service.ISceneService;
import com.chamberlain.common.annotation.Log;
import com.chamberlain.common.core.controller.BaseController;
import com.chamberlain.common.core.domain.AjaxResult;
import com.chamberlain.common.core.page.TableDataInfo;
import com.chamberlain.common.enums.BusinessType;
import com.chamberlain.common.utils.poi.ExcelUtil;
import com.chamberlain.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 *
 * @author: CYY
 * @date: 2024/3/25
 */
@RestController
@RequestMapping("/equipment/scene")
public class SceneController extends BaseController {
    @Autowired
    private ISceneService sceneService;

    @Autowired
    private ISysUserService userService;


    /**
     * 查询场景列表
     */
    @PreAuthorize("@ss.hasPermi('equipment:scene:list')")
    @GetMapping("/list")
    public TableDataInfo list(Scene scene)
    {
        startPage();
        List<Scene> list = sceneService.selectSceneList(scene);
        return getDataTable(list);
    }

    /**
     * 导出场景列表
     */
    @PreAuthorize("@ss.hasPermi('equipment:scene:export')")
    @Log(title = "场景", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Scene scene) {
        List<Scene> list = sceneService.selectSceneList(scene);
        ExcelUtil<Scene> util = new ExcelUtil<Scene>(Scene.class);
        util.exportExcel(response, list, "场景数据");
    }

    /**
     * 获取场景详细信息
     */
    @PreAuthorize("@ss.hasPermi('equipment:scene:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(sceneService.selectSceneById(id));
    }

    /**
     * 新增场景
     */
    @PreAuthorize("@ss.hasPermi('equipment:scene:add')")
    @Log(title = "场景", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Scene scene) {
        LoginUser loginUser = getLoginUser();
        return toAjax(sceneService.insertScene(scene,loginUser));
    }

    /**
     * 修改场景
     */
    @PreAuthorize("@ss.hasPermi('equipment:scene:edit')")
    @Log(title = "场景", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Scene scene) {
        return toAjax(sceneService.updateScene(scene));
    }

    /**
     * 删除场景
     */
    @PreAuthorize("@ss.hasPermi('equipment:scene:remove')")
    @Log(title = "场景", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        Long userId = getUserId();
        return toAjax(sceneService.deleteSceneByIds(ids,userId));
    }


    @GetMapping(value = "/userSceneList")
    public AjaxResult getUserSceneList() {
        Long userId = getUserId();
        SysUser sysUser=new SysUser(userId);
        List<Scene> list = sceneService.selectSceneListByUser(sysUser);
        return AjaxResult.success(list);
    }
}